import time, random
setGlobalDuration(-1)

beginPythonInst(1)
soundfont(duration=2)
endPythonInst()

proc = startCsound()

scl = scale('C')

time.sleep(1)

# example 1 is chaotic behaviours
# example 2 presents strong patterns and changing values
example = 2

logi = logistic(3.76, .1)
logi2 = logistic(3.5, .1)

def pit():
    x = logi.next()
    dict = {'soundfont': {}}
    dict['soundfont']['midipitch'] = scl[int(mapper(x,0,1,12,60))]
    sendEvent(1, dict, proc)

x1 = x2 = x3 = x4 = 0
count = 0
def pit2():
    global x1, x2, x3, x4, count
    val = logi2.next()
    x = scl[int(mapper(val,0,1,12,60))]
    if x == x4:
        count += 1
    else:
        count = 0
    dict = {'soundfont': {}}
    dict['soundfont']['midipitch'] = x
    sendEvent(1, dict, proc)
 
    x1, x2, x3, x4 = x, x1, x2, x3
    # If a pattern of 4 notes repeats 2 times, we change the constant and initial values
    if count == 8:
        count = 0
        logi2.changeInitial(random.choice([.1,.7,.5]))
        logi2.changeConstant(random.choice([3.5, 3.25, 3]))

if example == 1:
    ti1 = pattern(.125, pit, [1])
    ti1.start()
    ti1.play()
elif example == 2:
    ti1 = pattern(.125, pit2, [1])
    ti1.start()
    ti1.play()


